﻿Public Class ucTxtCurr
    Dim DecDigits As String

    Public Sub initialize(DecimalDigits As Integer)
        DecDigits = DecimalDigits.ToString()
    End Sub

    Private Sub txtcurr_Enter(sender As Object, e As EventArgs) Handles txtcurr.Enter
        If (IsNumeric(txtcurr.Text)) Then
            txtcurr.Text = Convert.ToDecimal(txtcurr.Text).ToString()
            BeginInvoke(DirectCast(Sub() txtcurr.SelectAll(), Action))
        End If
    End Sub

    Private Sub txtcurr_Leave(sender As Object, e As EventArgs) Handles txtcurr.Leave
        If (IsNumeric(txtcurr.Text)) Then
            If (DecDigits Is Nothing) Then
                DecDigits = "2"
            End If
            txtcurr.Text = Convert.ToDecimal(txtcurr.Text).ToString("N" + DecDigits, System.Globalization.CultureInfo.CreateSpecificCulture("en-US"))
        End If
    End Sub

    Private Sub txtcurr_Validating(sender As Object, e As System.ComponentModel.CancelEventArgs) Handles txtcurr.Validating
        If (txtcurr.Text.Trim() <> "") Then
            If (IsNumeric(txtcurr.Text) = False) Then
                e.Cancel = True
            End If
        End If
    End Sub

    Private Sub txtcurr_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtcurr.KeyPress
        Dim FullStop As Char
        FullStop = "."
        If e.KeyChar = FullStop And txtcurr.Text.IndexOf(FullStop) <> -1 Then
            e.Handled = True
            Return
        End If
        If Not Char.IsDigit(e.KeyChar) Then
            If (e.KeyChar <> FullStop) And
               (e.KeyChar <> Convert.ToChar(Keys.Back)) Then
                e.Handled = True
                Return
            End If
        End If
    End Sub
End Class
